<?php
/**
 * Created by PhpStorm.
 * User: sunshubo
 * Date: 2017/9/20
 * Time: 19:54
 */

class Login_model extends MY_Model
{
    /**
     * @param $data
     * @return string
     */
    function login($data)
    {
        $phone = $data['name'];
        $pass1 = $data['pass'];
        $pass2 = substr($data['pass'], -6);
        $sql = /** @lang text */
            "SELECT id,name,powerId,pass FROM ws_user WHERE phone='$phone'";

        $query = $this->db->query($sql);
        $error = $this->db->error();
        if (!$query->row()) {//记录不存在
            return json_encode(array('result' => ERROR, 'msg' => '账号或密码不正确！', 'code' => $error['code']));
        } else {
            if ($pass2 === $query->row()->pass || $pass1 === $query->row()->pass) {
                //账号且密码正确
                $exp = $this->update_token($query->row()->id, $query->row()->name, $query->row()->pass, $query->row()->powerId, $phone);
                if ($exp) {
                    return json_encode(array(
                        'result' => SUCCESS,
                        'msg' => '欢迎登录文思学堂！',
                        'p' => $query->row()->powerId,
                        'exp' => $exp,
                        'info' => array(
                            'name' => $query->row()->name,
                            'id' => $query->row()->id
                        )
                    ));
                }
            }
            return json_encode(array('result' => ERROR, 'msg' => '账号或密码不正确！', 'code' => $error['code']));
        }
    }

    /**
     * @param $id
     * @param $name
     * @param $pass
     * @param $powerId
     * @param $phone
     * @return bool
     */
    function update_token($id, $name, $pass, $powerId, $phone)
    {
        //账号存在
        //更新token,expire
        $token = md5($name . $pass . rand(10, 1000) . $phone . time());
        $expire = time();
        $sql = /** @lang text */
            "UPDATE ws_user SET token='$token' , expire='$expire' WHERE id=" . $id;
        //sql事物
        $this->db->trans_start();
        $query = $this->db->query($sql);
        $this->db->trans_complete();
        $error = $this->db->error();
        session_start();
        $_SESSION[$token] = array('id' => $id, 'name' => $name, 'expire' => $expire);
//        var_dump($_SESSION);
//        $this->set_session($token, array('id' => $id, 'name' => $name, 'expire' => $expire));
        if ($query) {
            //set cookie
            $cookie = array(
                'name' => '_wsxt',
                'value' => $token,
                'expire' => time() + 1800,
            );
            $this->input->set_cookie($cookie);
            $cookie = array(
                'name' => 'uid',
                'value' => 'us' . $expire,
                'expire' => time() + 1800,
            );
            $this->input->set_cookie($cookie);
            $cookie = array(
                'name' => 'sid',
                'value' => $id,
                'expire' => time() + 1800,
            );
            $this->input->set_cookie($cookie);
            $cookie = array(
                'name' => 'name',
                'value' => $name,
                'expire' => time() + 1800,
            );
            $this->input->set_cookie($cookie);
            return $expire;
        }
        return False;
    }

    function set_session($key, $value)
    {
        $this->session->set_userdata($key, $value);
    }
}